#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    long long subArrayRanges(vector<int>& nums)
    {
        long long res = 0;
        for (int i = 0; i < nums.size(); ++i)
        {
            int Max = nums[i];
            int Min = nums[i];
            long long sum = 0;
            for (int j = i + 1; j < nums.size(); ++j)
            {
                Max = max(Max, nums[j]);
                Min = min(Min, nums[j]);
                sum += Max - Min;
            }
            res += sum;
        }
        return res;
    }
};